package com.dianning.web.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dianning.bean.AdAds;
import com.dianning.bean.Company;
import com.dianning.bean.Money;
import com.dianning.bean.Profile;
import com.dianning.bean.Resume;
import com.dianning.bean.UserDetail;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Util;
import com.dianning.exception.DianningException;
import com.dianning.service.AdService;
import com.dianning.service.Cache;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.web.interceptor.RemoteAddrAware;

public class Ad extends BaseAction{

	private static final Log logger = LogFactory.getLog(Ad.class);

	private AdService adService;
	private Cache adCache;

	public AdService getAdService() {
		return adService;
	}

	public void setAdService(AdService adService) {
		this.adService = adService;
	}
	
	public Cache getAdCache() {
		return adCache;
	}

	public void setAdCache(Cache adCache) {
		this.adCache = adCache;
	}
	
	
	private String code;
	private String callback;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
	public String getCallback() {
		return callback;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}
	
	private String json;
	
	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}

	public String index(){
		List ads = this.getAdService().findAdsByCode(this.code, 1, "addTime", 1);
		if(ads == null){
			if(this.getCallback() == null){
				this.setJson("[]");
			}else{
				this.setJson(this.getCallback() + "([])");
			}
			return SUCCESS;
		}
		JSONArray adjsons = new JSONArray();
		try {
			for(int i=0;i<ads.size();i++){
				AdAds ad = (AdAds) ads.get(i);
				JSONObject js = new JSONObject();
				js.put("id", ad.getId());
				js.put("name", ad.getName());
				js.put("note", ad.getNote());
				js.put("tel", ad.getTel());
				js.put("pic", ad.getPic());
				js.put("direct", ad.getDirect());
				adjsons.put(js);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if(this.getCallback() == null){
			this.setJson("[" + adjsons.toString().substring(1, adjsons.toString().length()-1) + "]");
		}else{
			this.setJson(this.getCallback() + "([" + adjsons.toString().substring(1, adjsons.toString().length()-1) + "])");
		}
		//logger.info(this.getJson());
		return SUCCESS;
	}
	
	public String delcache(){
		logger.info("ad cache del");
		this.getAdCache().removeAll();
		if(this.getCallback() == null){
			this.setJson("{del:'true'}");
		}else{
			this.setJson("({del:'true'})");
		}
		return SUCCESS;
	}

}
